/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 4 $
 * @lastrevision  $Date: 2011-09-28 17:12:13 +0000 (Wed, 28 Sep 2011) $	 
 */
package com.proyecto404.core.io {
	import com.proyecto404.core.BaseObject;
	import com.proyecto404.core.Check;
	import com.proyecto404.core.EventArgs;
	import com.proyecto404.core.exception.FileNotFoundException;
	import com.proyecto404.core.io.DisplayObjectLoadedEventArgs;
	import com.proyecto404.core.log.fireflash.Console;
	
	import flash.display.Loader;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	
	import org.osflash.signals.ISignal;
	import org.osflash.signals.Signal;

	public class DisplayObjectLoader extends BaseObject {
		private var _loadError:Signal;
		private var _progress:Signal;
		private var _loaded:Signal;
		
		public function DisplayObjectLoader() {
			_loadError = new Signal(EventArgs);
			_progress = new Signal(FileLoadProgressEventArgs);
			_loaded = new Signal(DisplayObjectLoadedEventArgs);
		}
		
		public function get loadError():ISignal {
			return _loadError;
		}
		
		public function get progress():ISignal {
			return _progress;
		}

		public function get loaded():ISignal {
			return _loaded;
		}

		public function load(file:String):void {
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:ErrorEvent):void {
				Console.debug("[DisplayObjectLoader] no se ha podido cargar el archivo " + file);
				_loadError.dispatch(new LoadErrorEventArgs(file));
			});
			
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, function(e:ProgressEvent):void {
				_progress.dispatch(new FileLoadProgressEventArgs(e.bytesLoaded, e.bytesTotal));
			});
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
				_loaded.dispatch(new DisplayObjectLoadedEventArgs(file, loader.content));
			});
			//loader.load(new URLRequest(file), new LoaderContext(false, ApplicationDomain.currentDomain));
			loader.load(new URLRequest(file));
		}

		public function loadBytes(bytes:ByteArray):void {
			// TODO: Ver de generalizar la carga de bytes
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:ErrorEvent):void {
				Console.debug("[DisplayObjectLoader] no se ha podido efectuar la carga");
				_loadError.dispatch(new LoadErrorEventArgs(""));
			});
			
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, function(e:ProgressEvent):void {
				_progress.dispatch(new FileLoadProgressEventArgs(e.bytesLoaded, e.bytesTotal));
			});
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
				_loaded.dispatch(new DisplayObjectLoadedEventArgs("", loader.content));
			});
			//loader.load(new URLRequest(file), new LoaderContext(false, ApplicationDomain.currentDomain));
			var context:LoaderContext = new LoaderContext();
			context.allowCodeImport = true;
			loader.loadBytes(bytes, context);
		}

	}
}